package com.itheima.controller;

import com.itheima.annotation.LogOperation;
import com.itheima.pojo.*;
import com.itheima.result.PageResult;
import com.itheima.result.Result;
import com.itheima.service.StudentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping ("/students")
@RequiredArgsConstructor
public class StudentController {

    /**
     * 注入service
     */
    private final StudentService studentService;
//    @Autowired
//    private StudentService studentService;

    /**
     * 分页查询
     */
    @GetMapping
    public Result page(StudentQueryString studentQueryString) {
        log.info("分页查询学员信息" );
        PageResult pageResult = studentService.queryString(studentQueryString);
        return Result.success(pageResult);
    }

    /**
     * 删除学生信息
     */
    @DeleteMapping  ("/{ids}")
    public Result delete(@PathVariable("ids")List<Integer> ids){
        log.info("删除员工信息：{}" ,ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 添加学生信息
     */
    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("添加学生信息:{}",student);
        studentService.add(student);
        return Result.success();
    }

    /**
     * 根据id查询学生信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询学生信息:{}",id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学生信息
     */
    @LogOperation
    @PutMapping
    public Result alter(@RequestBody Student student){
        log.info("修改学生信息:{}",student);
        studentService.alter(student);
        return Result.success();
    }

    /**
     * 违纪处理
     */
    @PutMapping("/violation/{id}/{score}")
    public  Result handle(@PathVariable Integer id,@PathVariable Integer score){
        log.info("违纪处理:{},{}",id,score);
        studentService.handle(id,score);
        return Result.success();
    }
}
